<template>
  <el-container>
    <el-aside width="200px">
      <div class="sidebar-header">
        <img src="/images/logo.png" alt="logo" class="logo" />
        <div class="title">
          <h2>乡村振兴</h2>
          <p>农户管理系统</p>
        </div>
      </div>
      <el-menu
        :default-active="activeMenu"
        router
        class="el-menu-vertical"
        background-color="#304156"
        text-color="#bfcbd9"
        active-text-color="#409EFF"
      >
        <el-menu-item index="/farmer/home">
          <el-icon><HomeFilled /></el-icon>
          <span>首页</span>
        </el-menu-item>
        <el-menu-item index="/farmer/products">
          <el-icon><Goods /></el-icon>
          <span>农产品管理</span>
        </el-menu-item>
        <el-menu-item index="/farmer/attractions">
          <el-icon><Location /></el-icon>
          <span>乡村旅游景点</span>
        </el-menu-item>
        <el-menu-item index="/farmer/ai-chat">
          <el-icon><ChatDotRound /></el-icon>
          <span>智能体AI</span>
        </el-menu-item>
        <el-menu-item index="/farmer/orders">
          <el-icon><List /></el-icon>
          <span>订单管理</span>
        </el-menu-item>
        <el-menu-item index="/farmer/profile">
          <el-icon><User /></el-icon>
          <span>个人中心</span>
        </el-menu-item>
      </el-menu>
    </el-aside>

    <el-container>
      <el-header>
        <div class="header-content">
          <div class="breadcrumb">
            <el-breadcrumb>
              <el-breadcrumb-item>农户管理系统</el-breadcrumb-item>
              <el-breadcrumb-item>{{ currentPath }}</el-breadcrumb-item>
            </el-breadcrumb>
          </div>
          <div class="user-info">
            <el-dropdown @command="handleCommand">
              <span class="user-dropdown">
                <el-avatar :size="32" :src="userInfo.avatar" />
                <span>{{ userInfo.userName }}</span>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="profile"
                    >个人信息</el-dropdown-item
                  >
                  <el-dropdown-item command="logout">退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </div>
      </el-header>

      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from "vue";
import { useRoute, useRouter } from "vue-router";
import {
  HomeFilled,
  Goods,
  List,
  User,
  Location,
  ChatDotRound,
} from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";

const route = useRoute();
const router = useRouter();

const userInfo = ref({
  userName: "",
  avatar: "",
});

// 当前激活的菜单项
const activeMenu = computed(() => route.path);

// 当前页面标题
const currentPath = computed(() => {
  const pathMap: Record<string, string> = {
    "/farmer/home": "首页",
    "/farmer/products": "农产品管理",
    "/farmer/attractions": "乡村旅游景点",
    "/farmer/ai-chat": "AI助农",
    "/farmer/orders": "订单管理",
    "/farmer/profile": "个人中心",
  };
  return pathMap[route.path] || "";
});

// 获取用户信息
const getUserInfo = async () => {
  try {
    const res = await fetch("/api/user/rural/queryInfo", {
      headers: {
        Authorization: `logos ${localStorage.getItem("token")}`,
      },
    }).then((r) => r.json());

    if (res.success) {
      userInfo.value = res.data;
    }
  } catch (error) {
    console.error("获取用户信息失败:", error);
  }
};

// 处理下拉菜单命令
const handleCommand = (command: string) => {
  if (command === "logout") {
    localStorage.removeItem("token");
    router.push("/login");
    ElMessage.success("退出成功");
  } else if (command === "profile") {
    router.push("/farmer/profile");
  }
};

// 组件挂载时获取用户信息
onMounted(() => {
  getUserInfo();
});
</script>

<style lang="scss" scoped>
.el-container {
  height: 100vh;
  background-color: #f5f7fa;
}

.el-aside {
  width: 220px !important;
  background-color: #304156;
  transition: width 0.3s;
  box-shadow: 2px 0 6px rgba(0, 21, 41, 0.15);

  .sidebar-header {
    height: 80px;
    padding: 16px;
    display: flex;
    align-items: center;
    background: #263445;
    margin-bottom: 8px;

    .logo {
      width: 40px;
      height: 40px;
      margin-right: 12px;
    }

    .title {
      color: #fff;

      h2 {
        margin: 0;
        font-size: 16px;
        font-weight: 600;
        line-height: 1.5;
      }

      p {
        margin: 0;
        font-size: 12px;
        color: #a7b1bd;
      }
    }
  }

  .el-menu {
    border-right: none;

    .el-menu-item {
      height: 56px;
      line-height: 56px;
      margin: 4px 0;
      padding: 0 16px !important;

      &:hover {
        background-color: #263445 !important;
      }

      &.is-active {
        background-color: #1890ff !important;
        color: #fff !important;

        &::before {
          content: "";
          position: absolute;
          left: 0;
          top: 0;
          bottom: 0;
          width: 4px;
          background: #fff;
        }

        .el-icon {
          color: #fff !important;
        }
      }

      .el-icon {
        font-size: 18px;
        margin-right: 12px;
        color: inherit;
      }

      span {
        font-size: 14px;
      }
    }
  }
}

.el-header {
  height: 60px;
  background-color: #fff;
  border-bottom: 1px solid #f0f0f0;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  padding: 0;
  position: relative;
  z-index: 10;

  .header-content {
    max-width: 1400px;
    margin: 0 auto;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 24px;

    .breadcrumb {
      .el-breadcrumb {
        line-height: 60px;

        :deep(.el-breadcrumb__item) {
          .el-breadcrumb__inner {
            color: #666;
            &.is-link:hover {
              color: #1890ff;
            }
          }
          &:last-child {
            .el-breadcrumb__inner {
              color: #333;
              font-weight: 600;
            }
          }
        }
      }
    }

    .user-info {
      .user-dropdown {
        display: flex;
        align-items: center;
        padding: 0 12px;
        cursor: pointer;
        transition: all 0.3s;
        border-radius: 4px;

        &:hover {
          background: rgba(0, 0, 0, 0.025);
        }

        .el-avatar {
          margin-right: 8px;
          border: 2px solid #fff;
          box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        span {
          font-size: 14px;
          color: #666;
        }
      }
    }
  }
}

.el-main {
  padding: 24px;
  overflow-x: hidden;

  :deep(.el-card) {
    border-radius: 8px;
    border: none;
    box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);

    .el-card__header {
      padding: 16px 20px;
      border-bottom: 1px solid #f0f0f0;
    }

    .el-card__body {
      padding: 20px;
    }
  }
}
</style> 